<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0001																	/
/																							/
/========================================== JooSam ========================================*/

// Joomla security
defined('_JEXEC') or die;
 
## Import Joomla Data
jimport('joomla.application.component.controllerform');
jimport('joomla.database.table');

## Text String controller
class JooSamControllerTextstring extends JControllerForm{
	protected $text_prefix = 'Textstring';

	## Save button
	public function save($key = null, $urlVar = null){

		$data = JRequest::getVar('jform', array(), 'post', 'array');
		
		## New text string
		if ($data['id'] == '0'){
			$build = $this->strNew();

			if ($build == 'defstr'){
				$this->setMessage(JText::_('COM_JOOSAM_TXTSTRING_ERROR_DEFAULT_TXT'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else if ($build == 'ourofrange'){
				$this->setMessage(JText::_('COM_JOOSAM_TXTSTRING_TOLOW_TOBIG'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else if ($build == 'exist'){
				$this->setMessage(JText::_('COM_JOOSAM_TXTSTRING_CODE_EXISTS'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else if ($build == 'pass'){
				$this->setMessage(JText::_('COM_JOOSAM_TXTSTRING_SAVED'));
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else if ($build == 'errors'){
				$this->setMessage(JText::_('COM_JOOSAM_UNKNOWN_ERROR'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else{
				$this->setMessage(JText::_('COM_JOOSAM_UNKNOWN_ERROR'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
		}
		
		## Edit text string
		else{
			$build = $this->strEdit();

			if ($build == 'defstr'){
				$this->setMessage(JText::_('COM_JOOSAM_TXTSTRING_ERROR_DEFAULT_TXT'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else if ($build == 'editdone'){
				$this->setMessage(JText::_('COM_JOOSAM_EDIT_DONE'));
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else if ($build == 'ourofrange'){
				$this->setMessage(JText::_('COM_JOOSAM_TXTSTRING_TOLOW_TOBIG'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else if ($build == 'errors'){
				$this->setMessage(JText::_('COM_JOOSAM_UNKNOWN_ERROR'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else if ($build == 'doublerr'){
				$this->setMessage(JText::_('COM_JOOSAM_TXTSTRING_CODE_EXISTS'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
			else{
				$this->setMessage(JText::_('COM_JOOSAM_UNKNOWN_ERROR'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textstrings', false));
			}
		}
	}
	
	## Work (new)
	protected function strNew(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');
		$def_string = 'Enter your text here.';
		$chk_error = $this->checkError();
		$samerror = $data['samerror'];
		$txtstring = $data['txtstring'];
		if ($data['txtstring'] == $def_string){ $result = 'defstr'; }
		else if ($data['samerror'] < '100' OR $data['samerror'] > '9999'){ $result = 'ourofrange'; }
		else if ($chk_error != 'no'){ $result = 'exist'; }
		else if ($chk_error == 'no'){
			$db =& JFactory::getDBO();
			$query = "
				INSERT INTO #__joosam_txt (samerror,txtstring)
				VALUES ('".$samerror."','".$txtstring."')";
			$db->setQuery($query);
			$db->query();
			$result = 'pass'; 
		}
		else{ $result == 'errors'; }
		return $result;
	}
	
	## Work (edit)
	protected function strEdit(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');	
		$def_string = 'Enter your text here.';
		$chk_error = $this->checkError();
		if ($data['txtstring'] == $def_string){ $result = 'defstr'; }
		else if ($data['samerror'] < '100' OR $data['samerror'] > '9999'){ $result = 'ourofrange'; }
		else if ($data['id'] == $chk_error OR $chk_error == 'no' ){
			$db =& JFactory::getDBO();
			$query = "
				UPDATE ".$db->nameQuote('#__joosam_txt')."
				SET ".$db->nameQuote('txtstring')." = ".$db->quote($data['txtstring']).",
					".$db->nameQuote('samerror')." = ".$db->quote($data['samerror'])."
				WHERE ".$db->nameQuote('id')." = ".$db->quote($data['id'])." AND ".$db->nameQuote('isdef')." = ".$db->quote($data['isdef']).";
			";
			$db->setQuery($query);
			$db->query();
			$result = 'editdone';
		}
		else if ($data['id'] != $chk_error AND $chk_error != 'no' ){ $result = 'doublerr'; }
		else{ $result == 'errors'; }
		return $result;
	}

	## Work (returncode checker
	protected function checkError(){
		$data = JRequest::getVar('jform', array(), 'post', 'array');
		$db =& JFactory::getDBO();
		$query = "
			SELECT ".$db->nameQuote('id')."
			FROM ".$db->nameQuote('#__joosam_txt')."
			WHERE ".$db->nameQuote('samerror')." = ".$data['samerror'].";
		";
		$db->setQuery($query);
		$result = $db->loadResult();
		if ($result == ''){ $exist = 'no'; }
		else{ $exist = $result; }
		return $exist;
	}
}
?>